<template>
  <el-dialog :title="title" :visible.sync="isShow" width="40%" :show-close="false" :close-on-click-modal="false" :close-on-press-escape="false" @open="openEvent">
    <el-form :model="form" :rules="rules" style="width: 70%;margin: 0px 15%;">
      <el-form-item label="用户名称" :label-width="formLabelWidth">
        <el-input v-model="form.userLoginName" autocomplete="off" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="原密码" :label-width="formLabelWidth">
        <el-input v-model="form.oldPassword" label="原密码" show-password></el-input>
      </el-form-item>
      <el-form-item label="新密码" :label-width="formLabelWidth">
        <el-input v-model="form.newPassword" label="新密码" show-password></el-input>
      </el-form-item>
      <el-form-item label="确认密码" :label-width="formLabelWidth">
        <el-input v-model="form.confirmPassword" label="确认密码" show-password></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="operateInfo">确 定</el-button>
    </div>
  </el-dialog>
</template>
        
  <script>
import { changePwd } from '@/api/user'

export default {
  name: 'ChangePwdComp',
  data () {
    const validatePassword = (rule, value, callback) => {
      if (value !== form.newPassword) {
        callback(new Error('新密码与确认密码不一致'))
      } else {
        callback()
      }
    }
    return {
      form: {
        userLoginName: '',
        oldPassword: "",
        newPassword: '',
        confirmPassword: ""
      },
      rules: {
        oldPassword: [{ required: true, message: '原密码不能为空', trigger: 'blur' }],
        newPassword: [{ required: true, message: '原密码不能为空', trigger: 'blur' }],
        confirmPassword: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      formLabelWidth: '80px',
      title: '修改密码',
    }
  },
  props: {
    isShow: {
      type: Boolean,
      required: true
    },
    loginName: {
      type: String,
      require: false
    }
  },
  methods: {
    openEvent () {
      this.form.userLoginName = this.loginName
      this.title = '修改密码'
    },
    closeDialog () {
      this.$emit("changeDialogStatus", false);
    },
    result (data) {
      this.$emit("changeDialogStatus", "success");
      if (data.status === 200) {
        this.$message.success("操作成功");
      } else {
        this.$message.error("操作失败，请重试");
      }
    },
    operateInfo () {
      const param = { ...this.form }
      changePwd(param).then((res) => {
        this.result(res)
      });
    }
  }
}
</script>
    
<style lang="scss" scoped>
$bg: white;
::v-deep .el-form-item {
  background: white !important;
}

::v-deep .el-input {
  background: whitesmoke !important;
  color: black;
}
::v-deep .el-input input {
  color: black !important;
}
</style>
        